import mayDebt from '../../api/mayDebt.js'
import date from '../../utils/date.js'
import toast from '../../utils/toast.js'
import moment from 'moment'
const values = {
	user: '',
	user_id: '', //债权人或债务人
	loan_money: '', //借款金额
	// loan_date: '', //借款日期
	repay_type: '一次性还款', //还款方式
	year_rate: '', //年化利率
	loan_date:'2021-09-24',
	repay_date: '2021-09-24', //还款日期，一次性还款必填
	cycle: '', //周期（天）
	cycle_num: '', //期数
	remarks: '', //备注
	install: '',
	preview: '0',
	total_money: '0',
    loanpurpose: '',
	zztype: ''
}
const form={
	pay_money:'',
	pay_remarks:'出借人自愿免除部分债务',
}
const dateForm={
	year:'',
	month:'',
	days:'',
	sup_num:0,
	new_cycle:'',
	debt_id:'',
	next_pay_date:'',
}
const state = {
	rejectshow:{
		show:false
	},
	status: 'loadmore',
	dateForm:JSON.parse(JSON.stringify(dateForm)),
	selectList: [
		{
			label:'还款日正序',
			value:'repay_date'
		   },
		{
		 label:'还款日倒序',
		 value:'repay_date desc'
		},
		{
		 label:'金额递增',
		 value:'loan_money'
		},
		{
			label:'金额递减',
			value:'loan_money desc'
		   },
	],
	listTop: [{
		name: '全部'
	}, {
		name: '进行中'
	}, {
		name: '已逾期',
	}],
	creditorsList: [{
			name: '全部'
		}, {
			name: '待确认'
		}, {
			name: '进行中'
		},
		{
			name: '已完结',
		},
		{
			name: '已逾期',
		}
		
	],
	content: [{
			text: '待确认',
			checked: true,
			amount:10,
		},
		{
			text: '发起',
			checked: false,
		},
	],
	crediContent:[
		{
				text: '待签署',
				checked: true,
				amount:0,
			},
			{
					text: '待确认',
					checked: true,
					amount:0,
				},
			{
				text: '发起',
				checked: false,
			},
	],
	rows: [],
	total: 0,
	total_money: 0,
	confirmtotal: '0',
	receipt: {
		creditor: {
			username: ''
		},
		obligor:{
			username:''
		}
	},
	repay_record: [],
	values: JSON.parse(JSON.stringify(values)),
	rules: {
		user: [{
			required: true,
			message: '请选择债权人',
			trigger: 'blur,change'
		}, ],
		year_rate: [{
				required: true,
				message: '请输入年化利率',
				trigger: 'blur,change'
			},
			{  
				pattern:  /^[0-9]+\.?[0-9]{0,2}$/g,
				// 正则检验前先将值转为字符串
				transform(value) {
					return String(value);
				},
				message: '利率范围：0~15.4%'
			},
		],
		loan_money:[
			{
				required: true,
				message: '请输入欠款金额',
				trigger: 'blur,change'
			},
		],
		loanpurpose: [{required: true, message: '请选择借款用途'}],
		zztype: [{required: true, message: '请选择转账方式'}]
	},
	creditorRules: {
		user: [{
			required: true,
			message: '请选择债务人',
			trigger: 'blur,change'
			}],
		year_rate: [{
				required: true,
				message: '请输入年化利率',
				trigger: 'blur,change'
			},
			{  
				pattern:  /^[0-9]+\.?[0-9]{0,2}$/g,
				// 正则检验前先将值转为字符串
				transform(value) {
					return String(value);
				},
				message: '利率范围：0~15.4%'
			},
		],
		loan_money:[
			{
				required: true,
				message: '请输入合约金额',
				trigger: 'blur,change'
			},
			{
				pattern:  /^\d+(\.\d+)?$/g,
				// 正则检验前先将值转为字符串
				transform(value) {
					return String(value);
				},
				message: '请正确填写合约金额'
			},
		],
		loanpurpose: [{required: true, message: '请选择借款用途'}],
		zztype: [{required: true, message: '请选择转账方式'}]
	},
	actionSheetList: [],
	settingList: [{
			label: '一次性还款',
			value: '1'
		},
		{
			label: '分期还款',
			value: '2'
		},

	],
	list: [],
	unconfirmed:[],
	flexible: [
		[{
				label: '分1期',
				value: '1'
			},
			{
				label: '分2期',
				value: '2'
			},
			{
				label: '分3期',
				value: '3'
			},
			{
				label: '分4期',
				value: '4'
			},
			{
				label: '分5期',
				value: '5'
			},
			{
				label: '分6期',
				value: '6'
			},
			{
				label: '分7期',
				value: '7'
			},
			{
				label: '分8期',
				value: '8'
			},
			{
				label: '分9期',
				value: '9'
			},

			{
				label: '分10期',
				value: '10'
			},
			{
				label: '分11期',
				value: '11'
			},
			{
				label: '分12期',
				value: '12'
			},
			{
				label: '分13期',
				value: '13'
			},
			{
				label: '分14期',
				value: '14'
			},
			{
				label: '分15期',
				value: '15'
			},
			{
				label: '分16期',
				value: '16'
			},
			{
				label: '分17期',
				value: '17'
			},
			{
				label: '分18期',
				value: '18'
			},
			{
				label: '分19期',
				value: '19'
			},
			{
				label: '分20期',
				value: '20'
			},
			{
				label: '分21期',
				value: '21'
			},
			{
				label: '分22期',
				value: '22'
			},
			{
				label: '分23期',
				value: '23'
			},

			{
				label: '分24期',
				value: '24'
			},
			{
				label: '分25期',
				value: '25'
			},
			{
				label: '分26期',
				value: '26'
			},
			{
				label: '分27期',
				value: '27'
			},
			{
				label: '分28期',
				value: '28'
			},
			{
				label: '分28期',
				value: '28'
			},
			{
				label: '分29期',
				value: '29'
			},
			{
				label: '分30期',
				value: '30'
			},
			{
				label: '分31期',
				value: '31'
			},
			{
				label: '分32期',
				value: '32'
			},
			{
				label: '分33期',
				value: '33'
			},
			{
				label: '分34期',
				value: '34'
			},
			{
				label: '分35期',
				value: '35'
			},
			{
				label: '分36期',
				value: '36'
			},

			{
				label: '分37期',
				value: '37'
			},
			{
				label: '分38期',
				value: '38'
			},
			{
				label: '分39期',
				value: '39'
			},
			{
				label: '分40期',
				value: '40'
			},
			{
				label: '分41期',
				value: '41'
			},
			{
				label: '分42期',
				value: '42'
			},
			{
				label: '分43期',
				value: '43'
			},
			{
				label: '分44期',
				value: '44'
			},
			{
				label: '分45期',
				value: '45'
			},
			{
				label: '分46期',
				value: '46'
			},
			{
				label: '分47期',
				value: '47'
			},
			{
				label: '分48期',
				value: '48'
			},
			{
				label: '分49期',
				value: '49'
			},
			{
				label: '分50期',
				value: '50'
			},

			{
				label: '分51期',
				value: '51'
			},
			{
				label: '分52期',
				value: '52'
			},
			{
				label: '分53期',
				value: '53'
			},
			{
				label: '分54期',
				value: '54'
			},
			{
				label: '分55期',
				value: '55'
			},
			{
				label: '分56期',
				value: '56'
			},
			{
				label: '分57期',
				value: '57'
			},
			{
				label: '分58期',
				value: '58'
			},
			{
				label: '分59期',
				value: '59'
			},
			{
				label: '分60期',
				value: '60'
			},
		],
		[{
			value: '',
			label: '共1天'
		}, ],
		[{
				label: '每期1天',
				value: '1'
			},
			{
				label: '每期2天',
				value: '2'
			},
			{
				label: '每期3天',
				value: '3'
			},
			{
				label: '每期4天',
				value: '4'
			},
			{
				value: '5',
				label: '每期5天'
			},
			{
				value: '6',
				label: '每期6天'
			},
			{
				value: '7',
				label: '每期7天'
			},
			{
				value: '8',
				label: '每期8天'
			},
			{
				value: '9',
				label: '每期9天'
			},
			{
				value: '10',
				label: '每期10天'
			},
			{
				value: '11',
				label: '每期11天'
			},
			{
				value: '12',
				label: '每期12天'
			},
			{
				value: '13',
				label: '每期13天'
			},
			{
				value: '14',
				label: '每期14天'
			},
			{
				value: '15',
				label: '每期15天'
			},
			{
				value: '16',
				label: '每期16天'
			},
			{
				value: '17',
				label: '每期17天'
			},
			{
				value: '18',
				label: '每期18天'
			},
			{
				value: '19',
				label: '每期19天'
			},
			{
				value: '20',
				label: '每期20天'
			},
			{
				value: '21',
				label: '每期21天'
			},
			{
				value: '22',
				label: '每期22天'
			},
			{
				value: '23',
				label: '每期23天'
			},
			{
				value: '24',
				label: '每期24天'
			},
			{
				value: '25',
				label: '每期25天'
			},
			{
				value: '26',
				label: '每期26天'
			},
			{
				value: '27',
				label: '每期27天'
			},
			{
				value: '28',
				label: '每期28天'
			},
			{
				value: '29',
				label: '每期29天'
			},
			{
				value: '30',
				label: '每期30天'
			},
		],
	],
	repaymentDate:[{
		label: '每期1天',
		value: '1'
	},
	{
		label: '每期2天',
		value: '2'
	},
	{
		label: '每期3天',
		value: '3'
	},
	{
		label: '每期4天',
		value: '4'
	},
	{
		value: '5',
		label: '每期5天'
	},
	{
		value: '6',
		label: '每期6天'
	},
	{
		value: '7',
		label: '每期7天'
	},
	{
		value: '8',
		label: '每期8天'
	},
	{
		value: '9',
		label: '每期9天'
	},
	{
		value: '10',
		label: '每期10天'
	},
	{
		value: '11',
		label: '每期11天'
	},
	{
		value: '12',
		label: '每期12天'
	},
	{
		value: '13',
		label: '每期13天'
	},
	{
		value: '14',
		label: '每期14天'
	},
	{
		value: '15',
		label: '每期15天'
	},
	{
		value: '16',
		label: '每期16天'
	},
	{
		value: '17',
		label: '每期17天'
	},
	{
		value: '18',
		label: '每期18天'
	},
	{
		value: '19',
		label: '每期19天'
	},
	{
		value: '20',
		label: '每期20天'
	},
	{
		value: '21',
		label: '每期21天'
	},
	{
		value: '22',
		label: '每期22天'
	},
	{
		value: '23',
		label: '每期23天'
	},
	{
		value: '24',
		label: '每期24天'
	},
	{
		value: '25',
		label: '每期25天'
	},
	{
		value: '26',
		label: '每期26天'
	},
	{
		value: '27',
		label: '每期27天'
	},
	{
		value: '28',
		label: '每期28天'
	},

],
	mensem: [
		[{
				value: '0',
				label: '0年'
			},
			{
				value: '1',
				label: '1年'
			},
			{
				value: '2',
				label: '2年'
			},
			{
				value: '3',
				label: '3年'
			},
			{
				value: '4',
				label: '4年'
			},
		],
		[{
			value: '',
			label: ''
		}, ],
		[{
			value: '0',
			label: '0个月'
		},
			{
				value: '1',
				label: '1个月'
			},
			{
				value: '2',
				label: '2个月'
			},
			{
				value: '3',
				label: '3个月'
			},
			{
				value: '4',
				label: '4个月'
			},
			{
				value: '5',
				label: '5个月'
			},
			{
				value: '6',
				label: '6个月'
			},
			{
				value: '7',
				label: '7个月'
			},
			{
				value: '8',
				label: '8个月'
			},
			{
				value: '9',
				label: '9个月'
			},
			{
				value: '10',
				label: '10个月'
			},
			{
				value: '11',
				label: '11个月'
			},
			{
				value: '12',
				label: '12个月'
			},
		]
	],
   editValues:JSON.parse(JSON.stringify(values)),
    schedule:{},
	dialogVisible:{
		show:false,
		whole:false
	},
	waitRecord:[],
	loading:false,
	form:JSON.parse(JSON.stringify(form)),
	startDate: '2021-08-20',
	endDate: "2022-09-20",
	index:0,
	nostatus:'loadmore',
	unTotal:0,
}
const mutations = {
	ASSIGNMENT_REMARKES(state,data){
		console.log(data,111111)
	 state.values.remarks=data	
	},
	//更改驳回弹框状态
	CHANGE_REJECT_SHOW(state,data){
     state.rejectshow.show=data
	},
	CHANGE_VISBLE(state){
     state.dialogVisible={
		show:false,
		whole:false
	 }
	},  
	INITIALIZATION(state){
     state.values=JSON.parse(JSON.stringify(values))
	} ,
	CHANGE_NO_STATUS(state,data){
      state.nostatus=data
	},
	//一次性还款
	ALTERATION_DATE(state,data){
     let date=data.split('-')
	 state.dateForm.year=date[0]
	 state.dateForm.month=~~date[1]
	 state.dateForm.days=~~date[2]
	 state.dateForm.next_pay_date=data
	 state.dateForm.debt_id=state.receipt.id
	},
	CHANGE_STATUS(state, data) {
		state.status = data
	},
	//灵活分期
	CHANGE_STATRT_TIME(state){
		let time =  moment(new Date()).add(60, "months");
		state.endDate =  moment(time).format('YYYY-MM-DD')
		state.startDate =  moment(new Date()).format('YYYY-MM-DD')

	},
	GET_CYCLE(state,data){
		state.dateForm.debt_id=state.receipt.id
		state.dateForm.new_cycle=data
	},
	//获取变更日期
	GET_PERIODS(state,data){
		state.dateForm.sup_num=data
	},
	GET_YEAR(state,data){
		 state.dateForm.year=data
		 state.dateForm.sup_num=Number(data)*12+Number(state.dateForm.month)
	},

	GET_MONTH(state,data){
		 state.dateForm.month=data
		 state.dateForm.sup_num=Number(data)+Number(state.dateForm.year)*12
	},
	GET_DAYS(state,data){
		 state.dateForm.days=data
		 state.dateForm.debt_id=state.receipt.id
		 let date=state.receipt.repay_date.split('-')
		 if(state.dateForm.days<10){
		  state.dateForm.next_pay_date=`${date[0]}-${date[1]}-0${state.dateForm.days}`
		 }else{
			 state.dateForm.next_pay_date=`${date[0]}-${date[1]}-${state.dateForm.days}`	
		 }
	},
	AGAIN_LIST(state) {
		state.list[1][0].label = "共一天"
	},
	//获取还款日期
	PICKER_DATA(state, data) {
		state.values.repay_date = data
	},
	//拿到合约开始时间
	CONTRACT_PICKER_DATE(state,data){
		state.values.loan_date = data
	},
	//保存周期跟期数
	SAVE_CYCLE(state, data) {
		if(state.index==0){
		state.values.cycle = data[2].value
		state.values.cycle_num = data[0].value
		state.values.install = state.list[1][0].label		
		}else{
		state.values.cycle = data[2].value
		state.values.cycle_num = Number(data[0].value)*12+Number(data[2].value)
		state.values.install = state.list[1][0].label	
		}
	},
	CHANGE_STAGES(state, val) {
		state.values.repay_type = val
	},
	CHANGE_INSTALLMENT(state, time) {
		var d = new Date().getDate();
		state.mensem[1][0].label = `1个月,${d}号还款`
		state.values.repay_date = time
		var timeout =moment(new Date()).format('YYYY-MM-DD')
		console.log(timeout)
		state.values.loan_date=timeout
	},
	ASSIGNMENT(state, val) {
		state.values.user_id = val.value
		var name=val.label.trim().split(" ");
		state.values.user = name[0]
	},
	CHANGE_LIST_DATA(state, data) {

		if (state.index == 0) {
			state.list[1][0].label = `共${data[0].value*data[2].value}天`
		} else {
			var d = new Date().getDate();
			let number = Number(data[0].value * 12) + Number(data[2].value)
			let val = ~~number
			state.list[1][0].label = `${val}个月,${d}号还款`
		}
	},
	//灵活分期
	SWITCH_FLEXIBLE(state) {
		state.list = JSON.parse(JSON.stringify(state.flexible))
	},
	SWITCH_MONTHLY(state) {
		state.list = JSON.parse(JSON.stringify(state.mensem))
	},
	
	SAVE_INDEX(state,data){
		if(data==1){
			var d = new Date().getDate();
         state.mensem[1][0].label=`0个月,${d}号还款`
		}else{
		state.flexible[1][0].label='共1天'	
		}
     state.index=data
	},
	//债务编辑合约数据回显
	EDIT_DEBIT(state,data){
		var d = new Date().getDate();
		state.values={
			user:data.creditor.username,
			user_id:data.creditor_id,
			loan_money:data.loan_money,//借款金额
			repay_date:data.repay_date, //还款日期
			total_money:data.total_money,
			year_rate:String(data.year_rate),
			remarks:data.remarks,
			debt_id:data.id,
			cycle_num:data.cycle_num,
			cycle:data.cycle
		}
		if(data.repay_type=='1'){
			state.values.repay_type='一次性还款'
		}else{
			state.values.repay_type='分期还款'
			if(data.repay_type=='2'){
				state.values.install=`${data.cycle_num}个月,${d}号还款`
				state.index=0
			}else if(data.repay_type=='3'){
				state.values.install=`共${data.days}天` 
				state.index=1
			}
		}
	},
	//债权合约编辑数据回显	
	EDIT_OBLIGOR_DEBIT(state,data){
		var d = new Date().getDate();
		state.values={
			user:data.obligor.username,
			user_id:data.obligor_id,
			loan_money:data.loan_money,//借款金额
			repay_date:data.repay_date, //还款日期
			total_money:data.total_money,
			year_rate:String(data.year_rate),
			remarks:data.remarks,
			debt_id:data.id,
			cycle_num:data.cycle_num,
			cycle:data.cycle
		}
		if(data.repay_type=='1'){
			state.values.repay_type='一次性还款'
		}else{
			state.values.repay_type='分期还款'
			if(data.repay_type=='2'){
				state.index=0
				state.values.install=`${data.cycle_num}个月,${d}号还款`
			}else if(data.repay_type=='3'){
				state.values.install=`共${data.days}天` 
				state.index=1
			}
		}
	},
	CHANGE_SHOW(state,data){
		state.form=JSON.parse(JSON.stringify(form))
		state.dialogVisible.show=data
	},
	CHANGE_WHOLE(state,data){
		state.dialogVisible.whole=data
	}
}
const actions = {
	// 验证支付密码
	CONFIRM_PAY_PWS({state, dispatch}, data)  {
		return new Promise((resolve, reject) => {
			mayDebt.confirmPayPassword(data).then(res => {
				if(res && res.code === 1) {
					resolve()
				} else {
					reject()
				}
			})
		})
		
	},
	//获取债务列表
	GET_DEBT({state}, data) {
		let values = JSON.parse(JSON.stringify(data))
		mayDebt.debtListPost(values).then(res => {
			if (res) {
				res.data.rows.map(item=>{
					item.loan_date=moment(item.loan_date).format('YYYY.MM.DD')
					item.repay_date=moment(item.repay_date).format('YYYY.MM.DD')
				})
				state.rows = res.data.rows
				state.loading=true
				state.total = res.data.total
				state.total_money = res.data.total_money
			}
		})
	},
	GET_NEW_DATA_MORE({state,dispatch},data){
		let values = JSON.parse(JSON.stringify(data))
        if (values.state == 0) {
			values.state = ''
		}
		mayDebt.debtListPost(values).then(res => {
			if (res) {
				res.data.rows.map(item=>{
					item.loan_date=moment(item.loan_date).format('YYYY.MM.DD')
					item.repay_date=moment(item.repay_date).format('YYYY.MM.DD')
				})
				state.rows = state.rows.concat(res.data.rows)
				state.loading=true
				state.total = res.data.total
				state.total_money = res.data.total_money
				if (data.page * data.page_size < res.total) {
					state.status= 'loadmore'
				} else {
					state.status= 'nomore'
				}
			}
		})
	},
	//获取债务未确认的数量
	GET_CONFIRMED({
		state
	},data) {
		data.state=0
		mayDebt.debtListPost(data).then(res => {
			if (res) {
				state.crediContent[1].amount=res.data.total
				state.content[0].amount=res.data.total
				state.confirmtotal = String(res.data.total)
				res.data.rows.map(item=>{
					item.loan_date=moment(item.loan_date).format('YYYY.MM.DD')
					item.repay_date=moment(item.repay_date).format('YYYY.MM.DD')
					item.createtime=moment(item.createtime*1000).format('YYYY.MM.DD HH:mm:ss')
				})
				state.unconfirmed=res.data.rows 
				state.unTotal=res.data.total
			}
		})
	},
	GET_CONFIRMED_MORE({state},data){
		mayDebt.debtListPost(data).then(res => {
			if (res) {
				state.crediContent[1].amount=res.data.total
				state.content[0].amount=res.data.total
				state.confirmtotal = String(res.data.total)
				res.data.rows.map(item=>{
					item.loan_date=moment(item.loan_date).format('YYYY.MM.DD')
					item.repay_date=moment(item.repay_date).format('YYYY.MM.DD')
					item.createtime=moment(item.createtime*1000).format('YYYY.MM.DD HH:mm:ss')
				})
				state.unconfirmed=state.unconfirmed.concat(res.data.rows) 
				if(data.page*data.page_size<res.data.total){
                  state.nostatus='loadmore'
				} else{
					state.nostatus='nomore'
				}
			}
		})
		
	},
	GET_NOTE_DETAILS({
		state
	}, id) {
		mayDebt.debtInfoPost({
			id: id
		}).then(res => {
			if (res) {
				res.data.creditor.mobile = date.phoneNumFilter(res.data.creditor.mobile)
				res.data.creditor.idcard = date.idCardFilter(res.data.creditor.idcard)
				if (res.data.repay_type == '1') {
					res.data.repay_type = '一次性还款'
				} else if (res.data.repay_type == '2') {
					res.data.repay_type = '按月分期'
				} else {
					res.data.repay_type = '灵活分期'
				}
				let time = res.data.repay_date.split('-')
				// res.data.interest=(res.data.total_money-res.data.loan_money).toFixed(2)
				res.data.date = ~~time[2]
				res.data.obligor.mobile = date.phoneNumFilter(res.data.obligor.mobile)
				res.data.obligor.idcard = date.idCardFilter(res.data.obligor.idcard)
				
				state.receipt = res.data
				state.receipt.year= Math.floor(parseFloat(res.data.cycle_num/12))
				state.receipt.month= parseInt(res.data.cycle_num%12);
				state.repay_record = res.data.repay_record
				
				// state.receipt.repay_status = ''
			}
		})
	},
	//获取债权人
	GET_DEBT_LIST({
		state
	}, data) {
		mayDebt.creditorListPost(data).then(res => {
			if (res) {
				res.data.map(item=>{
					item.name=item.username +'   ' + date.phoneNumFilter(item.mobile)
				})
				state.actionSheetList = res.data
				state.list = state.flexible
			}
		})
	},
	//获取债务人列表
	GET_OBLOGOR_LIST({state},data){
		mayDebt.obligorListPost(data).then(res => {
			if (res) {
				res.data.map(item=>{
					item.name=item.username +'   ' + date.phoneNumFilter(item.mobile)
				})
				state.actionSheetList = res.data
				state.list = state.flexible
			}
		})
	},
	//计算总额
	CALCULATE({
		state
	}, data) {
		mayDebt.createDebtPost(data).then(res => {
			if (res) {
				state.values.total_money = (res.data.total_money).toFixed(2)
			}
		})
	},
	//债务人创建合约
	SUBNIT_DEBIT({
		state
	}, index) {
		console.log(index);
		let data = JSON.parse(JSON.stringify(state.values))
		if (data.repay_type == '一次性还款') {
			data.repay_type = 1
		} else {
			if (index == 0) {
				data.repay_type = '3'
			} else {
				data.repay_type = '2'
			}
		}
		mayDebt.createDebtPost(data).then(res => {
			if (res) {
				toast.successTab('创建合约成功', '/pages/loanee/mayDebt/index')
				state.values = JSON.parse(JSON.stringify(values))
			}
		})
	},
	//债权人创建合约
	SUBNIT_DEBIT_OBLIOR({
		state
	}, index) {
		console.log(index);
		let data = JSON.parse(JSON.stringify(state.values))
		if (data.repay_type == '一次性还款') {
			data.repay_type = 1
		} else {
			
			if (index == 0) {
				data.repay_type = '3'
			} else {
				data.repay_type = '2'
			}
		}
		mayDebt.createDebtPost(data).then(res => {
			if (res) {
				toast.successTab('创建合约成功', '/pages/creditor/personam/index')
				state.values = JSON.parse(JSON.stringify(values))
			}
		})
	},
	//编辑合约
	SUBNIT_DEBIT_Edit({state},index){
		let data = JSON.parse(JSON.stringify(state.values))
		if (data.repay_type == '一次性还款') {
			data.repay_type = 1
		} else {
			if (state.index == 0) {
				data.repay_type = '3'
			} else {
				data.repay_type = '2'
			}
		}
		mayDebt.createDebtPost(data).then(res => {
			if (res) {
				mayDebt.debtDelPost({debt_id:data.debt_id}).then(date=>{
					if(date){
						toast.successTab('修改合约成功', '/pages/loanee/obligor/index')
						state.values = JSON.parse(JSON.stringify(values))
					}
				})
				
				
			}
		})
	},
	CONFIRM({state},data){
	 return	mayDebt.confirmDebtPost(data)
	},
	GET_SCHEDULE({state},data){
		mayDebt.cycleInfoPost({debt_id:data}).then(res=>{
			if(res){
			 state.schedule=res.data	
			 state.dialogVisible.show=true
			}
		})
	},
	//按期销账
	WRITEOFF_SCHEDULE({state,dispatch},data){
		mayDebt.cycleRepayPost({repay_id:data.scheduleId}).then(res=>{
					if(res){
						state.dialogVisible.show=false
						toast.success('销账成功')
						
						dispatch('GET_NOTE_DETAILS',data.id)
					}
				})
	},
	//自定义销账
	CUSTOM_REPAY({state,dispatch},id){
		state.form.debt_id=id
		mayDebt.customRepayPost(state.form).then(res=>{
			if(res){
			toast.success('销账成功')
			state.form=JSON.parse(JSON.stringify(form))
			state.dialogVisible.show=false	
			dispatch('GET_NOTE_DETAILS',id)
			}
		})
	},
	WRITE_OFF_ALL({state,dispatch},id){
		mayDebt.allRepayPost({debt_id:id}).then(res=>{
			if(res){
			toast.success('销账成功')
			state.dialogVisible.whole=false	
			dispatch('GET_NOTE_DETAILS',id)
			}
		})
	},
	//获取待签署合约列表
	GET_WAIT({state}){
	mayDebt.waitDebtPost().then(res=>{
		if(res){
			res.data.map(item=>{
				item.loan_date=moment(item.loan_date).format('YYYY.MM.DD')
				item.repay_date=moment(item.repay_date).format('YYYY.MM.DD')
				item.createtime=moment(item.createtime*1000).format('YYYY.MM.DD HH:mm:ss')
			})
			state.waitRecord=res.data
			state.crediContent[0].amount=res.data.length
		}
	})	
	},
	//编辑待签署合约
	EDIT_DEBIT_OBLIOR({state}){
		let data = JSON.parse(JSON.stringify(state.values))
	
		if (data.repay_type == '一次性还款') {
			data.repay_type = 1
		} else {
			if (state.index = 0) {
				data.repay_type = '3'
			} else {
				data.repay_type = '2'
			}
		}
		mayDebt.createDebtPost(data).then(res => {
			if (res) {
				mayDebt.debtDelPost({debt_id:data.debt_id}).then(date=>{
					if(date){
						toast.successTab('修改合约成功', '/pages/creditor/personam/index')
						state.values = JSON.parse(JSON.stringify(values))
					}
				})
				
				
			}
		})
	},
	REVDATE({state,dispatch}){
		mayDebt.updatedSubmitPost(state.dateForm).then(res=>{
			if(res){
				toast.success('变更日期成功')
				dispatch('GET_NOTE_DETAILS',state.receipt.id)
				state.dialogVisible.show=false
				state.dateForm=JSON.parse(JSON.stringify(dateForm))
			}
		})
	},
	//债务人驳回合约
	REJECT_DEBIT({state},data){
		mayDebt.debtDelPost({debt_id:data}).then(res=>{
			toast.successTab('驳回合约成功', '/pages/loanee/mayDebt/index')
		})
	},
	//债权人驳回合约
	REJECT_MAY_DEBIT({state},data){
		mayDebt.debtDelPost({debt_id:data}).then(res=>{
			toast.successTab('驳回合约成功', '/pages/creditor/personam/index')
		})
	}
}
export default {
	namespaced: true,
	state,
	mutations,
	actions
}
